package com.dkh.recommend.utils;



import com.dkh.recommend.entity.TbMusic;
import com.dkh.recommend.entity.TbRelate;
import com.dkh.recommend.utils.coreMaths.ItemCF;
import com.dkh.recommend.utils.coreMaths.UserCF;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class RecommendUtils {


    /**
     * userCF
     * 方法描述: 猜你喜欢
     * @param userId
     * @param TbRelatedataList  List<TbRelate>获取数据
     * @return
     */
//    public static List<TbMusic>  userCfRecommend(Long userId,List<TbRelate> data){
    public static List<Long>  userCfRecommend(Long userId,List<TbRelate> TbRelatedataList){

        List<Long> recommendations = UserCF.recommend(userId, TbRelatedataList);
        ////        根据推荐算法得出的结果Ids进行过滤，得出推荐音乐List
        return recommendations;
    }






    /**
     * 方法描述: 猜你喜欢
     * @param musicId 物品id
     * @param TbRelatedata
     * @return {@link List<TbMusic>}
     */
//    public static List<TbMusic>  itemCfRecommend(Long musicId, List<TbRelate> TbRelatedata){
        public static List<Long>  itemCfRecommend(Long musicId, List<TbRelate> TbRelatedata){
//        List<TbRelate> data= FileDataSource.getData();
        List<Long> recommendations = ItemCF.recommend(musicId, TbRelatedata);

////        根据推荐算法得出的结果Ids进行过滤，得出推荐音乐List
//        List<TbMusic> musicList = musicData.stream().filter(e -> recommendations.contains(e.getMusicId())).collect(Collectors.toList());
//        返回
        return recommendations;


    }


    /**
     * 根据关注列表获取关注
     * @param key
     * @param map
     * @param type
     * @return
     */
    public static Map<Long,Double> computeNeighbor(Long key, Map<Long, List<TbRelate>>  map, int type) {
//        public static<T,R> Map<Long,Double> computeNeighbor(Long key, Map<Long, List<R>>  map, int type) {
//        map<key,value> key:
        Map<Long,Double> distMap = new TreeMap<>();
        List<TbRelate> userItems=map.get(key);
        map.forEach((k,v)->{
            //排除此用户
            if(!k.equals(key)){
                //关系系数
                double coefficient = CoreMathsUtils.relateDist(v,userItems,type);
                //关系距离
                //   double distance=Math.abs(coefficient);
                distMap.put(k,coefficient);
            }
        });
        return distMap;
    }



}
